#pragma once

#include <iostream>
#include <poll.h>
#include <sys/time.h>
#include "Sockets.hpp"

using namespace std;

static const uint16_t defaultport = 8080;
const int fd_num_max = 128;
const int defaultfd = -1;

class PollServer
{
public:
    PollServer(uint16_t port = defaultport)
        : _port(port)
    {
        for (int i = 0; i < fd_num_max; i++)
            _event_fd[i].fd = defaultfd;
    }

    bool Init()
    {
        _listensock.Socket();
        _listensock.Bind(_port);
        _listensock.Listen();
    }

    void Start()
    {
        _event_fd[0].fd = _listensock.Fd();
        _event_fd[0].events = POLLIN;
        int timeout = 3000;
        for (;;)
        {
            int n = poll(_event_fd, fd_num_max, timeout);
            switch (n)
            {
            case 0:
                cout << "time out!" << endl;
                break;
            case -1:
                cerr << "select error!" << endl;
                break;
            default:
                cout << "Time is ready!!!" << endl;
                Dispatcher();
                break;
            }
        }
    }

    void Dispatcher()
    {
        for (int i = 0; i < fd_num_max; i++)
        {
            int fd = _event_fd[i].fd;
            if (fd == defaultfd)
                continue;

            if (_event_fd[i].revents & POLLIN)
            {
                if (fd == _listensock.Fd())
                    Accepter();
                else
                    Recver(fd, i);
            }
        }
    }

    void Accepter()
    {
        string clientip;
        uint16_t clientport;
        int sock = _listensock.Accept(&clientip, &clientport);
        if (sock < 0)
            return;
        log(Info, "accept success, %s: %d, sock fd: %d", clientip.c_str(), clientport, sock);

        int pos = 1;
        for (; pos < fd_num_max; pos++)
        {
            if (_event_fd[pos].fd != defaultfd)
                continue;
            else
                break;
        }
        if (pos == fd_num_max)
        {
            log(Warning, "server is full, close %d now!", sock);
            close(sock);
        }
        else
        {
            _event_fd[pos].fd = sock;
            _event_fd[pos].events = POLLIN;
            _event_fd->revents = 0;
            PrintFd();
        }
    }

    void Recver(int fd, int pos)
    {
        char buffer[1024];
        ssize_t n = read(fd, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            buffer[n] = 0;
            cout << "get a message: " << buffer;
        }
        else if (n == 0)
        {
            log(Info, "client quit, me too, close fd is : %d", fd);
            close(fd);
            _event_fd[pos].fd = defaultfd;
        }
        else
        {
            log(Warning, "read error, close fd is : %d", fd);
            close(fd);
            _event_fd[pos].fd = defaultfd;
        }
    }

    void PrintFd()
    {
        cout << "online fd list: ";
        for (int i = 0; i < fd_num_max; i++)
        {
            if (_event_fd[i].fd == defaultfd)
                continue;
            cout << _event_fd[i].fd << " ";
        }
        cout << endl;
    }

    ~PollServer()
    {
        _listensock.Close();
    }

private:
    Sockets _listensock;
    uint16_t _port;
    struct pollfd _event_fd[fd_num_max];
};